package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.scene.player.RoleModel;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.ResManager;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *帮派战战报逻辑 
	 * @author liuel
	 * 
	 */	
	public class FactionActionLogic
	{
		// 1 白 2 绿 3 蓝 4 紫 5 橙 6 暗金 7 ？ 8 红
		private const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#0070dd'>","#a335ee'>","#ff8000'>","#e5cc80'>","#ffff00'>","#ff0000'>"];
		private static var _self		:FactionActionLogic;
		private var _ui				:FactionActionUI;
		private var _model				:ModelLocator;
		//管理翻页
		private var _pageTurner		:NxTablePageTurner;
		//自己帮派名字
		private var _selfFactionName	:String;
		//保存排行榜的数组
		private var _rankVector		:Vector.<FactionwarfareVo>;
		//胜利帮派
		private var _winFaction		:String;
		public function FactionActionLogic()
		{
			
		}
		public static function getInstance():FactionActionLogic
		{
			if(_self == null)
			{
				_self = new FactionActionLogic();
			}
			return _self;
		}
		public function Init() : void
		{
			_ui				= FactionActionUI.getInstance();
			_model			= ModelLocator.getInstance();
			_rankVector		= new Vector.<FactionwarfareVo>();
			//接受数据打开节界面
			_model		.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_FIGHT_FIGHT_INFO),getInfo);
			_pageTurner	= new NxTablePageTurner(_ui.preBtn,_ui.nextBtn,_ui.nxTabel,_ui.pageLabel);
			_ui.nxTabel.dataProvider =null;

		}
		public function	BeforeShow():void
		{
			_rankVector		= new Vector.<FactionwarfareVo>();
			addEvent();
		}
		public function	AfterHide():void
		{
			_rankVector		= new Vector.<FactionwarfareVo>();
			removeEvent();
		}
		private function addEvent():void
		{
			_pageTurner		.addEventListener();
			_ui.windowBG	.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);	
			_ui.okBtn		.addEventListener(MouseEvent.CLICK,		onClickOK);
		}
		private function removeEvent():void
		{
			_pageTurner		.removeEventListener();
			_ui.windowBG	.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);	
			_ui.okBtn		.addEventListener(MouseEvent.CLICK,		onClickOK);
		}
		private function getInfo(e:CustomMsgEvent =null):void
		{
			var isAttack:Boolean =AttackLogic.getInstance().fighting
			if(isAttack)
				return;
			var factionArr:Array =[];
			factionArr =e.pList;
			var state:int = factionArr.shift();  //0 不显示  1胜利  2失败
			_winFaction = factionArr.shift();
			updateTaile(state);
			if(factionArr.length ==0)
			{
				_ui.nxTabel.visible =false;
			}
			else
			{
				_ui.nxTabel.visible =true;
				saveRank(factionArr);
				showLineInfo();
			}
			if(_ui.created == false)
			{
				_ui.showOrHide();
			}
		}
		private function updateTaile(val:int):void
		{
		
			var index:int;
			switch(val)
			{
				case 0:
					index = 1;
					break;
				case 1:
					index = 2;
					break;
				case 2:
					index =	3;
					break;
				
			}
			_ui.titalMC.gotoAndStop(index);
		}
		private function saveRank(arr:Array):void
		{
			_rankVector		= new Vector.<FactionwarfareVo>();
			for(var i:int=0;i<arr.length;i+=5)
			{
				var vo :FactionwarfareVo=new FactionwarfareVo(arr[i],arr[i+1],arr[i+2],arr[i+3],arr[i+4]);
				if(_rankVector.indexOf(vo.name) == -1)
				{
					_rankVector.push(vo);
				}
			}
			//按排名排序
			taxis(_rankVector);
		}
		private function showLineInfo():void
		{
			var itemArr:Array =[];
			var colcorID:int =0; 
			for(var i:String in _rankVector)
			{
				//是自己的帮派设置字体颜色
				if(_rankVector[i].factionname == _winFaction) //胜利
				{
					colcorID =1;
				}
				else
				{
					colcorID =8;
				}
				itemArr.push( [{label:link_color(String(int(i)+1),colcorID)},
						{label:link_color(_rankVector[i].name,colcorID)},{label:link_color(_rankVector[i].factionname,colcorID)},
						{label:link_color(_rankVector[i].num.toString(),colcorID)},{label:link_color(_rankVector[i].repute.toString(),colcorID)},
						{label:link_color(_rankVector[i].gold.toString(),colcorID)}]);
			}	
			_ui.nxTabel.dataProvider =itemArr;
		}
		/**
		 * 排序函数 
		 * @param vector
		 * 
		 */		
		private function taxis(vector:Vector.<FactionwarfareVo>):void
		{
			vector = vector.sort(compareFunc);
		}
		private function compareFunc(vo1:FactionwarfareVo,vo2:FactionwarfareVo):int
		{
			if(vo1.num == vo2.num)
			{
				if(vo1.factionname ==_winFaction)
				{
					return  -1;
				}
				else if(vo2.factionname == _winFaction)
				{
					return 1;
				}
				return -1;
			}
			else
			{
				return -(vo1.num-vo2.num);
			}
			
		}
		private function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		private function onClickOK(e:MouseEvent):void
		{
			_ui.hide();	
		}
		private function onClose(e:NxWindowEvent):void
		{
			_ui.hide();	
		}
	}
}
//战报
class FactionwarfareVo
{


	public var name		:String; 	 //玩家名字
	public var factionname	:String		//帮派名字
	public var num			:int;		//连胜
	public var	repute		:int;		//声望
	public var gold		:int;		//铜钱
	public function FactionwarfareVo(name:String,factionname:String,num:int,repute:int,gold:int):void
	{
		this.name	= name;
		this.factionname	= factionname;
		this.num	= num;
		this.repute	= repute;
		this.gold	= gold;
	}
}